using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TouchScreen : MonoBehaviour
{
	private Player player;

	public Transform pad;

	public Transform buttonsParent;

	public Sprite[] sprites;

	private bool istnieje;

	private Vector3 pozycjaDotyk;

	private int dotykIlosc;

	private List<Vector2> dotykPoczatek = new List<Vector2>();

	private List<int> dotykZakonczono = new List<int>();

	private void Start()
	{
		//Object.Destroy(this); 
		player = Library.player;
		for (int i = 0; i < 4; i++)
		{
			if (i < Progress.abilities.Length)
			{
				buttonsParent.GetChild(i).GetComponent<Image>().sprite = GetSprite(Progress.abilities[i].name);
			}
			else
			{
				Object.Destroy(buttonsParent.GetChild(i).gameObject);
			}
		}
	}

	private void Update()
	{
		int touchCount = Input.touchCount;
		bool flag = false;
		Vector2 direction = Vector2.zero;
		DotykKoniec();
		for (int i = 0; i < touchCount; i++)
		{
			Touch touch = Input.GetTouch(i);
			Vector3 vector = Camera.main.ScreenToViewportPoint(touch.position) - new Vector3(0.5f, 0.5f, 0f);
			vector = new Vector3(vector.x * 1920f, vector.y * 1080f, 0f);
			TouchPhase phase = touch.phase;
			if (phase == TouchPhase.Began || dotykIlosc <= i)
			{
				dotykIlosc++;
				dotykPoczatek.Insert(i, vector);
			}
			if (phase == TouchPhase.Canceled || phase == TouchPhase.Ended)
			{
				dotykZakonczono.Add(i);
			}
			if (Info.pause || Info.gameOver || vector.x > 370f)
			{
				continue;
			}
			if (touch.phase == TouchPhase.Began)
			{
				if (!istnieje)
				{
					istnieje = true;
					pozycjaDotyk = vector;
					pad.parent.gameObject.SetActive(true);
					pad.parent.GetChild(0).localPosition = vector;
					RuchGalka(vector);
					flag = true;
				}
			}
			else if ((touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) && istnieje && !flag)
			{
				if (Vector2.Distance(vector, pozycjaDotyk) > 20f)
				{
					direction = RuchGalka(vector);
				}
				else
				{
					pad.position = pad.parent.GetChild(0).position;
				}
				flag = true;
			}
		}
		if (istnieje && !flag)
		{
			istnieje = false;
			pad.parent.gameObject.SetActive(istnieje);
		}
		player.Move(direction);
	}

	private Vector2 RuchGalka(Vector3 pozycja)
	{
		Vector3 vector = pozycja - pozycjaDotyk;
		float magnitude = vector.magnitude;
		if (Mathf.Abs(magnitude) > 120f || Mathf.Abs(vector.x) > 120f)
		{
			pozycja = new Vector3(vector.x / magnitude * 120f, vector.y / magnitude * 120f) + pozycjaDotyk;
		}
		pad.localPosition = pozycja;
		vector = pozycja - pozycjaDotyk;
		return new Vector2(vector.x / 120f, vector.y / 120f);
	}

	private void DotykKoniec()
	{
		int count = dotykZakonczono.Count;
		if (count != 0)
		{
			for (int i = 0; i < count; i++)
			{
				dotykIlosc--;
				dotykPoczatek.RemoveAt(dotykZakonczono[i] - i);
			}
			dotykZakonczono.Clear();
		}
	}

	private Sprite GetSprite(string name)
	{
		Sprite[] array = sprites;
		foreach (Sprite sprite in array)
		{
			if (sprite.name == name)
			{
				return sprite;
			}
		}
		return null;
	}
}
